package com.black.app.service.impl.my;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.my.AppMyWeightDetail;
import com.black.app.domain.req.my.mydata.AppMyDataWeightDetailAddReq;
import com.black.app.domain.req.my.mydata.AppMyDataWeightDetailEditReq;
import com.black.app.service.my.AppMyWeightDetailService;
import com.black.app.mapper.my.AppMyWeightDetailMapper;
import com.black.common.exception.ServiceException;
import com.black.common.exception.database.PrimaryException;
import com.black.common.exception.database.RecordNotFoundException;
import com.black.common.utils.MessageUtils;
import com.black.common.utils.SecurityUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author 18494
* @description 针对表【app_my_weight_detail(体重记录详情表)】的数据库操作Service实现
* @createDate 2024-10-23 20:49:29
*/
@Service
public class AppMyWeightDetailServiceImpl extends ServiceImpl<AppMyWeightDetailMapper, AppMyWeightDetail>
    implements AppMyWeightDetailService{

    @Override
    public boolean add(AppMyDataWeightDetailAddReq req) {

        req.setUserId(SecurityUtils.getAppLoginUser().getUserId());

        // 判断日期是否存在
        if (this.baseMapper.exists(new LambdaQueryWrapper<AppMyWeightDetail>().eq(AppMyWeightDetail::getDate, req.getDate()))) {
            throw new ServiceException(MessageUtils.message("app.my.detail.date.exists"));
        }
        // 获取日期最大的详情
        AppMyWeightDetail maxEntity = this.baseMapper.selectOne(new LambdaQueryWrapper<AppMyWeightDetail>().orderByDesc(AppMyWeightDetail::getDate).last("limit 1"));

        if (maxEntity == null) {
            req.setEndData(0.0);
        }

        double maxData = maxEntity.getStateData();
        //取小数点后一位
        req.setEndData(req.getStateData() - maxData);

        AppMyWeightDetail newEntity = new AppMyWeightDetail();

        BeanUtils.copyProperties(req, newEntity);

        boolean save = this.save(newEntity);

        return save;
    }

    @Override
    public PageInfo<AppMyWeightDetail> getWeight(AppMyDataWeightDetailAddReq req) {

        req.setUserId(SecurityUtils.getAppLoginUser().getUserId());

        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.getWeight(req);
        });
    }

    @Override
    public boolean edit(AppMyDataWeightDetailEditReq req) {

        // 获取用户id
        req.setUserId(SecurityUtils.getAppLoginUser().getUserId());
        // 根据id查询
        AppMyWeightDetail db = this.getById(req.getId());
        // 判断 查不到数据
        if (Objects.isNull(db)) {
            throw new RecordNotFoundException();
        }
        // 获取AppMyWeightDetail表中所有的数据并且用户id等于req.getUserId，并且日期升序
        List<AppMyWeightDetail> list = this.baseMapper.selectList(new LambdaQueryWrapper<AppMyWeightDetail>()
                                        .orderByDesc(AppMyWeightDetail::getDate).eq(AppMyWeightDetail::getUserId, req.getUserId()));

        AppMyWeightDetail maxItem = list.get(1);

        req.setEndData(req.getStateData() - maxItem.getStateData());

        // 覆盖数据库中的数据
        BeanUtils.copyProperties(req, db);
        boolean result = this.updateById(db);

        return result;
    }

    @Override
    public boolean deleteById() {
        // 获取当前登录用户的id
        Long userId = SecurityUtils.getAppLoginUser().getUserId();
        // 删除AppMyWeightDetail表中的id最大的数据
        int id = this.baseMapper.selectOne(new LambdaQueryWrapper<AppMyWeightDetail>()
                    .eq(AppMyWeightDetail::getUserId, userId)
                    .orderByDesc(AppMyWeightDetail::getId).last("limit 1")).getId();
        // 删除
        boolean result = this.removeById(id);
        return result;
    }
}




